import wtforms
from wtforms.validators import length, email, EqualTo, input_required
from models import EmailCaptureModel, UserModel
import base64


class LoginForm(wtforms.Form):
    email = wtforms.SearchField(validators=[email()])
    password = wtforms.SearchField(validators=[length(min=6, max=20)])


class ResetPasswordForm(wtforms.Form):
    email = wtforms.SearchField(validators=[email()])
    password = wtforms.SearchField(validators=[length(min=6, max=20)])
    pwd_confirm = wtforms.SearchField(validators=[EqualTo('password')])


class RegisterForm(wtforms.Form):
    username = wtforms.SearchField(validators=[length(min=3, max=20)])
    email = wtforms.SearchField(validators=[email()])
    capture = wtforms.SearchField(validators=[length(min=4, max=4)])
    password = wtforms.SearchField(validators=[length(min=6, max=20)])
    pwd_confirm = wtforms.SearchField(validators=[EqualTo('password')])

    def validate_capture(self, field):
        capture = field.data
        email = self.email.data
        capture_model = EmailCaptureModel.query.filter_by(email=email).first()
        if not capture_model or capture_model.capture.lower() != capture.lower():
            raise wtforms.ValidationError("邮箱验证码错误。。。")

    def validate_email(self, field):
        email = field.data
        user_model = UserModel.query.filter_by(email=email).first()
        if user_model:
            raise wtforms.ValidationError("邮箱已经存在！！！")


class PhotoUpload(wtforms.Form):
    title = wtforms.SearchField(validators=[length(min=3, max=200)])
    content = wtforms.SearchField(validators=[length(min=5)])


class PictureShow(wtforms.Form):
    title = wtforms.SearchField(validators=[length(min=3, max=200)])
    content = wtforms.MultipleFileField(validators=[])
